package com.hzncc.scaffold.controller.Rear.Other.Chapter;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Chapter;
import com.hzncc.scaffold.entity.Other.Task;
import com.hzncc.scaffold.entity.Vo.ChapterIdVo;
import com.hzncc.scaffold.entity.Vo.InsertChapterVo;
import com.hzncc.scaffold.entity.Vo.UpdateChapterVo;
import com.hzncc.scaffold.service.ChapterService;
import com.hzncc.scaffold.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-21 10:41
 * @Description: null
 */
@RequestMapping("/manage/chapter")
@Api(tags = "后台接口-章节相关接口")
@RestController
public class ChapterController {

    @Autowired
    ChapterService chapterService;

    @Autowired
    TaskService taskService;

    @PostMapping("/insert")
    @ApiOperation("新增章节")
    public ResultT insert(@RequestBody InsertChapterVo insertChapterVo) {

        Chapter chapter = new Chapter();
        if (StrUtil.isNotEmpty(insertChapterVo.getTitle())) {
            chapter.setTitle(insertChapterVo.getTitle());
        }
        if (StrUtil.isNotEmpty(insertChapterVo.getRank())) {
            chapter.setRank(Integer.parseInt(insertChapterVo.getRank()));
        }
        if (StrUtil.isNotEmpty(insertChapterVo.getCourseId())) {
            chapter.setCourseId(Integer.parseInt(insertChapterVo.getCourseId()));
        }
        chapter.setCreateTime(DateUtil.now());

        boolean save = chapterService.save(chapter);

        if (save) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }


    @PostMapping("/update")
    @ApiOperation("修改章节")
    public ResultT update(@RequestBody UpdateChapterVo updateChapterVo) {
        Integer chapterId = updateChapterVo.getChapterId();

        if (chapterService.getById(chapterId) == null) {
            return ResultT.fail("数据错误");
        }

        Chapter chapter = chapterService.getById(chapterId);
        chapter.setTitle(updateChapterVo.getTitle());
        boolean update = chapterService.updateById(chapter);
        if (update) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @PostMapping("/delete")
    @ApiOperation("删除章节")
    public ResultT delete(@RequestBody ChapterIdVo chapterIdVo) {
        Integer chapterId = chapterIdVo.getChapterId();

        if (chapterService.getById(chapterId) == null) {
            return ResultT.fail("数据错误");
        }

        boolean remove = chapterService.removeById(chapterId);

        if (remove) {
            LambdaQueryWrapper<Task> lqw = new LambdaQueryWrapper<>();
            lqw.eq(chapterId != null, Task::getChapterId, chapterId);
            taskService.remove(lqw);
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }
}
